/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.nju.software.greencreditsystem.action;

import cn.edu.nju.software.greencreditsystem.DTO.InputStandardValueListDTO;
import cn.edu.nju.software.greencreditsystem.DTO.OutputStandardValueDTO;
import cn.edu.nju.software.greencreditsystem.business.LogisticPredictor;
import cn.edu.nju.software.greencreditsystem.domain.Enterprise;
import cn.edu.nju.software.greencreditsystem.domain.EnterpriseType;
import cn.edu.nju.software.greencreditsystem.domain.EnvironmentFactor;
import cn.edu.nju.software.greencreditsystem.domain.FinancialFactor;
import cn.edu.nju.software.greencreditsystem.domain.WarningTemplate;
import cn.edu.nju.software.greencreditsystem.factory.ServiceFactory;
import cn.edu.nju.software.greencreditsystem.service.ConfigService;
import cn.edu.nju.software.greencreditsystem.service.EnterpriseService;
import cn.edu.nju.software.greencreditsystem.service.EnterpriseTypeService;
import cn.edu.nju.software.greencreditsystem.service.EnvironmentService;
import cn.edu.nju.software.greencreditsystem.service.FinancialService;
import cn.edu.nju.software.greencreditsystem.service.WarningService;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.util.ServletContextAware;

/**
 * 作者：韩天
 * 创建时间：2011-8-22
 * 类描述：银行设置模块的action封装类
 */
public class BankConfigAction extends ActionSupport implements ServletContextAware {
    private int enterpriseId;//企业的id
    private List<Enterprise> enterpriseList;//所有企业list
    private List<EnterpriseType> typeList;//所有企业类型list
    private List<EnvironmentFactor> indexList;//环境指标list
    private List<EnvironmentFactor> scoreList;//环境评分list
    private List<FinancialFactor> factorList;//财务指标list
    private List<WarningTemplate> templateList;//警告模板list
    private List<OutputStandardValueDTO> outValueDTOList;//前台显示standardvalue
    private InputStandardValueListDTO valueListDTO;//接受前台数据的DTO
    private double leastLogisticCor;//最小相关系数
    private double leastLogisticHistory;//最少多少份历史记录建立模型
    private double environmentWeight;//客户评级时环境占比重
    private double financialWeight;//客户评价是财务占比重
    private double oneStar;
    private double twoStar;
    private double threeStar;
    private double fourStar;
    private WarningTemplate template;//修改warningtemplate
    
    private File upload;// 实际上传文件
    private String uploadContentType; // 文件的内容类型
    private String uploadFileName; // 上传文件名
    private String fileCaption;// 上传文件时的备注
    private ServletContext context;
    
    
    @Override
    /**
     * 方法描述：默认的执行方法，实现无条件跳转
     * param：
     * return：SUCCESS
     */
    public String execute(){
        return SUCCESS;
    }
    
    /**
     * 方法描述：银行跳转至设置主界面
     * param：
     * return：SUCCESS
     */
    public String lookup_Config(){
        EnterpriseService es = ServiceFactory.getEnterpriseServiceInstance();
        EnterpriseTypeService ets = ServiceFactory.getEnterpriseTypeServiceInstance();
        this.enterpriseList = es.getAllEnterprise();
        this.typeList = ets.getTypeList();
        
        return SUCCESS;
    }
    
    /**
     * 方法描述：银行跳转至环境指标设置界面
     * param：
     * return：SUCCESS
     */
    public String environment_Index(){
        EnvironmentService es = ServiceFactory.getEnvironmentServiceInstance();
        this.indexList = es.get_AllIndexFactor();
        ConfigService cs = ServiceFactory.getConfigServiceInstance();
        this.outValueDTOList = cs.get_EnIndexStandardValue(enterpriseId);
        return SUCCESS;
    }
    
    /**
     * 方法描述：银行跳转至环境评分设置界面
     * param：
     * return：SUCCESS
     */
    public String environment_Score(){
        EnvironmentService es = ServiceFactory.getEnvironmentServiceInstance();
        this.scoreList = es.get_AllScoreFactor();
        ConfigService cs = ServiceFactory.getConfigServiceInstance();
        this.outValueDTOList = cs.get_EnScoreStandardValue(enterpriseId);
        return SUCCESS;
    }
    
    /**
     * 方法描述：银行跳转至财务监控设置界面
     * param：
     * return：SUCCESS
     */
    public String financial_Factor(){
        FinancialService fs = ServiceFactory.getFinancialServiceInstance();
        this.factorList = fs.get_AllFinancialFactor();
        ConfigService cs = ServiceFactory.getConfigServiceInstance();
        this.outValueDTOList = cs.get_FiFactorStandardValue();
        return SUCCESS;
    }
    
    /**
     * 方法描述：银行跳转至财务预警设置界面
     * param：
     * return：SUCCESS
     */
    public String financial_Predicton(){
        ConfigService cs = ServiceFactory.getConfigServiceInstance();
        this.leastLogisticCor = cs.get_SystemConstant("leastLogisticCor");
        this.leastLogisticHistory = cs.get_SystemConstant("leastLogisticHistory");
        return SUCCESS;
    }
    
    /**
     * 方法描述：银行跳转至警告设置界面
     * param：
     * return：SUCCESS
     */
    public String warning(){
        WarningService ws = ServiceFactory.getWarningServiceInstance();
        this.templateList = ws.get_AllTemplate();
        return SUCCESS;
    }
    
    /**
     * 方法描述：银行跳转至客户设置界面
     * param：
     * return：SUCCESS
     */
    public String customer(){
        ConfigService cs = ServiceFactory.getConfigServiceInstance();
        this.environmentWeight = cs.get_SystemConstant("environmentWeight");
        this.financialWeight = cs.get_SystemConstant("financialWeight");
        this.oneStar = cs.get_SystemConstant("oneStar");
        this.twoStar = cs.get_SystemConstant("twoStar");
        this.threeStar = cs.get_SystemConstant("threeStar");
        this.fourStar = cs.get_SystemConstant("fourStar");
        return SUCCESS;
    }
    
    /**
     * 方法描述：银行更改环境指标设置
     * param：
     * return：SUCCESS
     */
    public String config_Environment_Index(){
        ConfigService cs = ServiceFactory.getConfigServiceInstance();
        cs.set_EnStandardValue(valueListDTO, enterpriseId);
        return SUCCESS;
    }
    /**
     * 方法描述：银行更改环境评分设置
     * param：
     * return：SUCCESS
     */
    public String config_Environment_Score(){
        ConfigService cs = ServiceFactory.getConfigServiceInstance();
        cs.set_EnStandardValue(valueListDTO, enterpriseId);
        return SUCCESS;
    }
    
    /**
     * 方法描述：银行更改财务监控设置
     * param：
     * return：SUCCESS
     */
    public String config_Financial_Factor(){
        ConfigService cs = ServiceFactory.getConfigServiceInstance();
        cs.set_FiStandardValue(valueListDTO);
        return SUCCESS;
    }
    
    /**
     * 方法描述：银行更改财务预警设置
     * param：
     * return：SUCCESS
     */
    public String config_Financial_Predicton(){
        ConfigService cs = ServiceFactory.getConfigServiceInstance();
        cs.set_SystemConstant("leastLogisticCor", leastLogisticCor);
        cs.set_SystemConstant("leastLogisticHistory", leastLogisticHistory);
        return SUCCESS;
    }
    
    /**
     * 方法描述：银行更改警告设置
     * param：
     * return：SUCCESS
     */
    public String config_Warning(){
        ConfigService cs = ServiceFactory.getConfigServiceInstance();
        cs.set_WarningTemplate(template);
        return SUCCESS;
    }
    
    /**
     * 方法描述：银行更改客户设置
     * param：
     * return：SUCCESS
     */
    public String config_Customer(){
        ConfigService cs = ServiceFactory.getConfigServiceInstance();
        cs.set_SystemConstant("environmentWeight", environmentWeight);
        cs.set_SystemConstant("financialWeight", financialWeight);
        cs.set_SystemConstant("oneStar", oneStar);
        cs.set_SystemConstant("twoStar", twoStar);
        cs.set_SystemConstant("threeStar", threeStar);
        cs.set_SystemConstant("fourStar", fourStar);
        return SUCCESS;
    }
    
    /**
     * 方法描述：银行上传财务历史报表初始化logistic模型
     * param：
     * return：SUCCESS
     */
    public String upload_FinancialHistory() throws Exception {
        try {
            String targetDirectory = context.getRealPath("/upload");
            String targetFileName = uploadFileName;

            File target = new File(targetDirectory, targetFileName);
            FileUtils.copyFile(upload, target);
            //用压缩包文件内的报表初始化logistic模型
            LogisticPredictor lp = new LogisticPredictor();
            lp.refreshHistory("D:/workspace/NetBeans7.0/trunk/GreenCreditSystem/src/java/excel-wole.xml", "\""+target.getPath()+"\"");
        } catch (Exception e) {
            e.printStackTrace();
            return INPUT;
        }
        return SUCCESS;
    }
    
    
    

    public List<Enterprise> getEnterpriseList() {
        return enterpriseList;
    }

    public void setEnterpriseList(List<Enterprise> enterpriseList) {
        this.enterpriseList = enterpriseList;
    }

    public List<EnvironmentFactor> getIndexList() {
        return indexList;
    }

    public void setIndexList(List<EnvironmentFactor> indexList) {
        this.indexList = indexList;
    }

    public List<EnvironmentFactor> getScoreList() {
        return scoreList;
    }

    public void setScoreList(List<EnvironmentFactor> scoreList) {
        this.scoreList = scoreList;
    }

    public List<EnterpriseType> getTypeList() {
        return typeList;
    }

    public void setTypeList(List<EnterpriseType> typeList) {
        this.typeList = typeList;
    }

    public List<FinancialFactor> getFactorList() {
        return factorList;
    }

    public void setFactorList(List<FinancialFactor> factorList) {
        this.factorList = factorList;
    }

    public List<WarningTemplate> getTemplateList() {
        return templateList;
    }

    public void setTemplateList(List<WarningTemplate> templateList) {
        this.templateList = templateList;
    }

    public InputStandardValueListDTO getValueListDTO() {
        return valueListDTO;
    }

    public void setValueListDTO(InputStandardValueListDTO valueListDTO) {
        this.valueListDTO = valueListDTO;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public double getLeastLogisticCor() {
        return leastLogisticCor;
    }

    public void setLeastLogisticCor(double leastLogisticCor) {
        this.leastLogisticCor = leastLogisticCor;
    }

    public double getLeastLogisticHistory() {
        return leastLogisticHistory;
    }

    public void setLeastLogisticHistory(double leastLogisticHistory) {
        this.leastLogisticHistory = leastLogisticHistory;
    }

    public List<OutputStandardValueDTO> getOutValueDTOList() {
        return outValueDTOList;
    }

    public void setOutValueDTOList(List<OutputStandardValueDTO> outValueDTOList) {
        this.outValueDTOList = outValueDTOList;
    }

    public double getEnvironmentWeight() {
        return environmentWeight;
    }

    public void setEnvironmentWeight(double environmentWeight) {
        this.environmentWeight = environmentWeight;
    }

    public double getFinancialWeight() {
        return financialWeight;
    }

    public void setFinancialWeight(double financialWeight) {
        this.financialWeight = financialWeight;
    }

    public double getFourStar() {
        return fourStar;
    }

    public void setFourStar(double fourStar) {
        this.fourStar = fourStar;
    }

    public double getOneStar() {
        return oneStar;
    }

    public void setOneStar(double oneStar) {
        this.oneStar = oneStar;
    }

    public double getThreeStar() {
        return threeStar;
    }

    public void setThreeStar(double threeStar) {
        this.threeStar = threeStar;
    }

    public double getTwoStar() {
        return twoStar;
    }

    public void setTwoStar(double twoStar) {
        this.twoStar = twoStar;
    }

    public ServletContext getContext() {
        return context;
    }

    public void setContext(ServletContext context) {
        this.context = context;
    }

    public String getFileCaption() {
        return fileCaption;
    }

    public void setFileCaption(String fileCaption) {
        this.fileCaption = fileCaption;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    @Override
    public void setServletContext(ServletContext context) {
        this.context = context;
    }

    public WarningTemplate getTemplate() {
        return template;
    }

    public void setTemplate(WarningTemplate template) {
        this.template = template;
    }
}
